package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.Equipmentinfo;
import com.ruoyi.business.mapper.EquipmentinfoMapper;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.DeviceConfigMainMapper;
import com.ruoyi.business.domain.DeviceConfigMain;
import com.ruoyi.business.service.IDeviceConfigMainService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 主设备配置Service业务层处理
 * 
 * @author DL
 * @date 2025-06-29
 */
@Service
public class DeviceConfigMainServiceImpl implements IDeviceConfigMainService 
{
    @Autowired
    private DeviceConfigMainMapper deviceConfigMainMapper;
    @Autowired
    private EquipmentinfoMapper equipmentinfoMapper;

    /**
     * 查询主设备配置
     * 
     * @param id 主设备配置主键
     * @return 主设备配置
     */
    @Override
    public DeviceConfigMain selectDeviceConfigMainById(Long id)
    {
        return deviceConfigMainMapper.selectDeviceConfigMainById(id);
    }

    /**
     * 查询主设备配置列表
     * 
     * @param deviceConfigMain 主设备配置
     * @return 主设备配置
     */
    @Override
    public List<DeviceConfigMain> selectDeviceConfigMainList(DeviceConfigMain deviceConfigMain)
    {
        return deviceConfigMainMapper.selectDeviceConfigMainList(deviceConfigMain);
    }

    /**
     * 新增主设备配置
     * 
     * @param deviceConfigMain 主设备配置
     * @return 结果
     */
    @Override
    public int insertDeviceConfigMain(DeviceConfigMain deviceConfigMain)
    {
        deviceConfigMain.setCreateTime(DateUtils.getNowDate());
        return deviceConfigMainMapper.insertDeviceConfigMain(deviceConfigMain);
    }

    /**
     * 修改主设备配置
     * 
     * @param deviceConfigMain 主设备配置
     * @return 结果
     */
    @Override
    @Transactional
    public int updateDeviceConfigMain(DeviceConfigMain deviceConfigMain)
    {
        int res = 0;
        String infoId = deviceConfigMain.getInfoId();
        String infoIdNew = deviceConfigMain.getInfoIdNew();
        // 修改主设备配置表
        deviceConfigMain.setUpdateTime(DateUtils.getNowDate());
        // 防止首次修改时InfoId为空，导致后面的修改不成功
        if(StringUtils.isEmpty(infoId)) {
            infoId = infoIdNew;
        }
        deviceConfigMain.setInfoId(infoIdNew);
        int res1 = deviceConfigMainMapper.updateDeviceConfigMain(deviceConfigMain);

        // 根据主设备配置表的info_id修改主设备台账表上个关联的设备台账的RFlag为0
        Equipmentinfo equipmentinfo1 = new Equipmentinfo();
        equipmentinfo1.setID(infoId);
        equipmentinfo1.setRFlag(0L);
        int res2 = equipmentinfoMapper.updateEquipmentinfo(equipmentinfo1);

        // 修改主设备台账表
        Equipmentinfo equipmentinfo2 = new Equipmentinfo();
        equipmentinfo2.setID(infoIdNew);
        equipmentinfo2.setEquipmentIp(deviceConfigMain.getDevAddress());
        equipmentinfo2.setRFlag(1L);
        int res3 = equipmentinfoMapper.updateEquipmentinfo(equipmentinfo2);
        if (res1 > 0 && res2 > 0 && res3 > 0) {
            res = 1;
        }
        return res;
    }

    /**
     * 批量删除主设备配置
     * 
     * @param ids 需要删除的主设备配置主键
     * @return 结果
     */
    @Override
    public int deleteDeviceConfigMainByIds(Long[] ids)
    {
        return deviceConfigMainMapper.deleteDeviceConfigMainByIds(ids);
    }

    /**
     * 删除主设备配置信息
     * 
     * @param id 主设备配置主键
     * @return 结果
     */
    @Override
    public int deleteDeviceConfigMainById(Long id)
    {
        return deviceConfigMainMapper.deleteDeviceConfigMainById(id);
    }
}
